package com.ymt.fmd.platform.service;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

@Service
public class FmdCacheService {

    /**
     * Logger
     */
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CacheManager cacheManager;

    //private String profile = env.getActiveProfiles()[0];

    /**
     * 查询缓存
     *
     * @param key 缓存键 不可为空
     **/
    public <T extends Object> T get(String cacheName, String key) {
        if (cacheManager==null) {
            log.info("get, No cacheManager bean.");  //dev
            return null;
        }
        if (StringUtils.isEmpty(key) == true) {
            return null;
        }
        Cache cache = cacheManager.getCache(cacheName);
        if (cache.get(key)!=null) {
            return (T)cache.get(key);
        }
        return null;
    }

    /**
     * 设置缓存键值  直接向缓存中插入值，这会直接覆盖掉给定键之前映射的值
     *
     * @param key 缓存键 不可为空
     * @param obj 缓存值 不可为空
     **/
    public <T extends Object> void put(String cacheName, String key, T obj) {
        if (cacheManager==null) {
            log.info("set, No cacheManager bean.");  //dev
            return;
        }
        Cache cache = cacheManager.getCache(cacheName);
        if (cache.get(key)!=null) {
            cache.put(key, obj);
        }
    }

    /**
     * 移除缓存
     *
     * @param key 缓存键 不可为空
     **/
    public void remove(String cacheName, String key) {
        if (cacheManager==null) {
            log.info("remove, No cacheManager bean.");  //dev
            return;
        }
        if (StringUtils.isEmpty(key) == true) {
            return;
        }
        Cache cache = cacheManager.getCache(cacheName);
        if (cache.get(key)!=null) {
            cache.evict(key);
        }
    }

    /**
     * 是否存在缓存
     *
     * @param key 缓存键 不可为空
     **/
    public boolean contains(String cacheName, String key) {

        // always return false in dev
//        if (log.isDebugEnabled()) {
//            log.debug("contains profile=" + profile);
//        }
//        if ("dev".equalsIgnoreCase(profile)) {
//            return false;
//        }
        if (cacheManager==null) {
            log.info("contains, No cacheManager bean.");  //dev
            return false;
        }

        boolean exists = false;

        if (StringUtils.isEmpty(key) == true) {
            return exists;
        }

        Cache cache = cacheManager.getCache(cacheName);
        if (cache.get(key)!=null) {
            return true;
        }

        return exists;
    }

    public String asKey(String... array) {
        return asKey('_', array);
    }

    public String asKey(char separator, String... array) {
        if (array == null) {
            return null;
        } else {
            int noOfItems = array.length;
            if (noOfItems <= 0) {
                return "";
            } else {
                StringBuilder buf = new StringBuilder(noOfItems * 16);
                for(int i = 0; i < noOfItems; i++) {
                    if (i > 0) {
                        buf.append(separator);
                    }
                    if (array[i] != null) {
                        buf.append(array[i]);
                    } else {
                        buf.append("");
                    }
                }
                return buf.toString();
            }
        }
    }

}
